<?php

class ApiController extends Controller
{
	public $layout  = "//layouts/home";
	public $pageKeyword = '';
	public $pageDesc = '';
	//导航焦点
	
	public $active_cur = '';
	/**
	 * 广告调用
	 */
	public function actionAdShow()
	{
		header('Content-type:application/x-javascript');
		$zid = Yii::app()->request->getParam('zid',0);
		$cid = Yii::app()->request->getParam('catid',0);
		$adzone = Adzone::model()->getData($zid,$cid);
		foreach($adzone['ads'] as $key=>$value){
			$adzone['ads'][$key]['zds_url'] = str_replace('//', '(-;-)', $value['zds_url']);
		}
		$this->renderPartial('adshow',array(
				'adzone'=>$adzone,
		));
		exit;
	}
	/**
	 * 广告统计
	 */
	public function actionAdClick()
	{
		$aid = Yii::app()->request->getParam('adseid',0);
		$url = $_GET['url'];
		$adzone = Adse::model()->updateCounters(array('zds_hits'=>1),'zds_id='.$aid);
		//echo $url;exit;
		$this->redirect(str_replace('(-;-)', '//', $url));
		exit;
	}

	public function actionCrmWcf()
	{	
		
		/** 
		* 设置参数array("trace"=>true)用于调试 
		* __getLastRequest 返回请求信息 
		* __getLastResponse 返回响应信息包括报错信息都会返回 
		 */ 
		try{  
			$server = "http://www.keyousoft.com/lzb_wcf/documentcenter.svc?wsdl";
		    $client = new SoapClient($server,array("trace"=>true));
		    $action = $_GET['action'];
		    switch ($action) {
		    	//根据会员号查询会员
			    case"AddCust";
			    $params = array('cardNo'=>'card8'); 
			    $webService = $client->GetCardByNo($params); 
			    $wsResult = $webService->GetCardByNoResult;
			    print_r(xml2array($wsResult));
			    break;
			    default:
			        exit(json_encode(error));
			}
		}catch(SoapFault $e){  
		    echo $client->__getLastRequest();  
		    echo $client->__getLastResponse();  
		    echo $e->getMessage();  
		}
		          
	}

	/*
	* API TOCRM
	*/
	public function actionShoptoCRM()
	{
		
		$action = !empty($_GET['action'])?$_GET['action']:'';
		$type   = !empty($_GET['type'])?$_GET['type']:'json';
		$key    = !empty($_POST['key'])?$_POST['key']:'';
		if($key == 'GHJGY7678HJJJHCRMTYYTIO566y89hrfewe'){			
			switch ($action) {
			    //注册会员
			    case"AddCust";			    	
			        $model = new Member;

			        if ($_POST['CustID'] == '' || $_POST['LoginName'] == '' || $_POST['Password'] == '') {			           
			            exit(json_encode('error')); //有空信息
			        }

			    	$model->zm_memberid =(int)$_POST['CustID'];//CRMID
			    	if(Member::model()->find('zm_memberid=:zm_memberid',array(':zm_memberid'=>$model->zm_memberid))){
			        	exit(json_encode(1)); //返回1表示CRMID重复
			        }	
			        $model->zm_name = safe_replace(trim($_POST['LoginName']));//登录名
			        if(Member::model()->find('zm_name=:zm_name',array(':zm_name'=>$model->zm_name))){
			        	exit(json_encode(-1)); //返回2表示登录名重复
			        }		       
			        $model->zm_paypsword = $model->passwordEncode($_POST['Password'] , 2);//支付密码
			        $model->zm_psword = $model->passwordEncode($_POST['Password']);//登录密码
			        $model->zm_code = $model->zm_code;//识别CODE
					$model->zm_reg_ip = Yii::app()->request->userHostAddress;//注册IP
			        $model->zm_email = trim($_POST['Email']);//注册Email
			        if(Member::model()->find('zm_email=:zm_email',array(':zm_email'=>$model->zm_email))){
			        	exit(json_encode(-3)); //返回3表示Email重复
			        }
			        $model->zm_mob   = trim($_POST['PhoneNo']);//电话号码
			        $model->zm_status = 2; //会员状态
					$model->updatetime = $model->inputtime = time(); //注册时间					
					if ($model->save()) {
			            exit(json_encode($model->zm_id)); //返回会员ID
			        }else{
			        	exit(json_encode(-100)); //返回会员ID
			        }
			        //写入会员附加信息表
					$mempro = new MemPro();
					$mempro->zm_id   = $model->zm_id;
			        $mempro->zmp_lastname = safe_replace($_POST['CustName']);
			        $mempro->zmp_nickname = safe_replace($_POST['CustAbbr']);
			        $mempro->zmp_sex = $_POST['Gender'];
			        $mempro->zmp_birthday = strtotime($_POST['Dirthday']);
			        $mempro->zmp_tel = $_POST['PhoneNo'];
			        $mempro->zmp_qq = !empty($_POST['QQ'])?$_POST['QQ']:'1000000';
			        $mempro->zmp_msn = $_POST['MSN'];
			        $mempro->zmp_siteurl = $_POST['HomePage'];
			        $mempro->zar_id      = 524 ;
			        $mempro->zmp_address = !empty($_POST['Address'])?$_POST['Address']:'广东深圳';
			        $mempro->zmp_zip = $_POST['Zip'];
			        $mempro->zmp_bank_name = $_POST['AccoutBank'];
			        $mempro->zmp_bank_user = $_POST['CustName'];
			        $mempro->zmp_bank_account = $_POST['AccoutNo'];
			        $mempro->save();		        					        
			        
			        break;
			    //查询用户信息
			    case"UpdateCust";
			        $model = Member::model()->find('zm_memberid ='.$_POST['CustID']);
			        if($_POST['Password']){
			        	$model->zm_psword = $model->passwordEncode($_POST['Password']);//登录密码
			        	$model->zm_code = $model->zm_code;//识别CODE
			        }		       
			        
			        if ($model->save()) {
			            exit(json_encode(1)); //返回会员ID
			        }else{
			        	exit(json_encode(-100)); //返回会员ID
			        }
			        break;
			    //添加产品
			    case"AddProduct";
			    	//实例化产品模型
					$model=new Product;
			        $model->zp_prodid      = (int)$_POST['prodID'];      //CRMID
			        $model->zp_product_no = $_POST['prodNo'];	  		//	        
			        $model->zp_name       = $_POST['prodName'];
			        $model->zp_price      = $_POST['prodPrice'];
			        $model->zm_id         = '1';
			        $model->zp_status     = '1';
			        $model->zp_state      = '1';
			        $model->zca_id        = '102';
			        if($model->save()){
						return True;
					}else{
						return False;
					}
			        break;
			    case "UpdateProduct";
			    	$model = Product::model()->find('zp_prodid ='.$_POST['prodID']);
			        $model->zp_prodid = $_POST['prodID'];
			        $model->zp_product_no = $_POST['prodNo'];			        
			        $model->zp_name = $_POST['prodName'];
			        $model->zp_price = $_POST['prodPrice'];
			        //$model->zm_id         = 1;		        
			        if($model->save()){
						return True;
					}else{
						return False;
					}			    	
			    	break;
			    default:
			        exit(json_encode(error));
			}
		}else{
			Message('接口KEY错误');
		}
	}

}